import { createRouter, createWebHistory } from 'vue-router'
import LoginView from '../views/LoginView.vue'
import HomeView from '../views/HomeView.vue'
import StudentList from '../views/StudentList.vue'
import TeacherList from '../views/TeacherList.vue'
import SchoolList from '../views/SchoolList.vue'
import CourseList from '../views/CourseList.vue'
import MainView from '../views/MainView.vue'
import PostView from '../views/PostView.vue'
import PostDetails from '../views/PostDetails.vue'
import { getToken } from '../utils/auth.js'
import { ElMessageBox } from 'element-plus'
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      name: 'login',
      component: LoginView
    },
    {
      path: '/',
      name: 'home',
      component: HomeView,
      children:[
        {
          path:"/student",
          component:StudentList
        },
        {
          path:"/teacher",
          component:TeacherList
        },
        {
          path:"/school",
          component:SchoolList
        },
        {
          path:"/course",
          component:CourseList
        },
        {
          path:"/main",
          name:"/main",
          component:MainView
        },
        {
          path:"/post",
          component:PostView
        },
        {
          path:"/postdetails",
          name:"/postdetails",
          component:PostDetails
        }
      ]
    }
  ]
})
router.beforeEach((to,from,next)=>{
  if(to.path == '/login'){
    next();
  }else{
    let token=getToken();
    if (!token) {
      ElMessageBox.alert('您还未登录,请先登录', '提示', {
        confirmButtonText: '确定'
      })
      next('/login');
    }else{
      next();
    }
  }
})



export default router
